/**
 * Filename: BannerAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/13 14:10
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.flaviofaria.kenburnsview.KenBurnsView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeCardBigPicActivity;
import com.github.postsanf.yinian.activity.TimeCardDetailActivity;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Banner适配器
 */
public class TimeCardpicAdapter extends PagerAdapter {
    private Activity mContext;
    private List<String> mAdList = new ArrayList<>();
    private ImageLoader imageLoader;
    private LayoutInflater inflater;

    public TimeCardpicAdapter(Activity mContext, List<String> mAdList) {
        this.mContext = mContext;
        this.mAdList = mAdList;
        imageLoader = ImageLoader.getInstance();
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mAdList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(
                R.layout.yn_item_image_gallerybrowse, null);
        final RelativeLayout rl_view_progrese = (RelativeLayout) view.findViewById(R.id.rl_view_progress);
        final KenBurnsView iv_image_browser = (KenBurnsView) view.findViewById(R.id.iv_image_browse);
        String url = mAdList.get(position);
//        String gurl = StringUtils.repaceImgUrl(YNUrlHelper.getGalleryUrl(mContext, mAdList.get(position)));
        Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).thumbnail(0.1f).into(iv_image_browser);
//        imageLoader.displayImage(url, iv_image_browser,
//                ImageOptHelper.getnormalOptions(), new SimpleImageLoadingListener() {
//                    @Override
//                    public void onLoadingStarted(String imageUri, View view) {
//                        rl_view_progrese.setVisibility(View.VISIBLE);
//                    }
//
//                    @Override
//                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                        String message = null;
//                        switch (failReason.getType()) {     // 获取图片失败类型
//                            case IO_ERROR:              // 文件I/O错误
//                                message = "文件错误";
//                                break;
//                            case DECODING_ERROR:        // 解码错误
//                                message = "解码错误";
//                                break;
//                            case NETWORK_DENIED:        // 网络延迟
//                                message = "网络延迟";
//                                break;
//                            case OUT_OF_MEMORY:         // 内存不足
//                                message = "内存不足";
//                                System.gc();
//                                break;
//                            case UNKNOWN:               // 原因不明
//                                message = "未知错误";
//                                break;
//                        }
//                        rl_view_progrese.setVisibility(View.GONE);
////						ToastUtils.showToast(mContext, message, Toast.LENGTH_SHORT);
////                        SnackBarUtils.makeShort(view, message).show();
//                    }
//
//                    @Override
//                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
////                            iv_image_browser.setImageBitmap(loadedImage);
//                        rl_view_progrese.setVisibility(View.GONE);       // 不显示进度条
//                    }
//                }, new ImageLoadingProgressListener() {
//                    @Override
//                    public void onProgressUpdate(String imageUri, View view,
//                                                 int current, int total) {
//                    }
//                });
        iv_image_browser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext, TimeCardBigPicActivity.class));
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // 这里不需要做任何事情
        System.gc();
    }
}